package net.htjs.juc.deadlock.v1;

import lombok.extern.slf4j.Slf4j;

/**
 * 活锁：两个线程互相改变对方的结束条件，导致两个线程谁也无法结束
 */
@Slf4j
public class LiveLockDemo {
    private static volatile int count  = 10;
    public static void main(String[] args) throws InterruptedException {
        new Thread(() -> {
            while (count > 0) {
                log.info("count: {}", count);
                try {
                    Thread.sleep(200);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                count--;
            }
        }, "t1").start();

        new Thread(() -> {
            while (count < 20) {
                log.info("count: {}", count);
                try {
                    Thread.sleep(200);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                count++;
            }
        }, "t2").start();
    }
}
